﻿<%@ WebHandler Language="C#" Class="ContainerStepDispatchHandler" %>

using System;
using System.Web;
using System.Collections.Generic;
using System.Web.Script.Serialization;
using System.Linq;
using BusinessRule.Business;
using System.Data;
using Newtonsoft.Json;
using System.IO;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using NPOI.HSSF.UserModel;
using System.Collections;
using BusinessRule.DTO;
using BusinessRule.IBusiness;
using BusinessRule.BusinessFactory;
using System.Web.SessionState;
using Camstar.WebClient.WebServicesProxy.InSiteWebServices;

//Description: 工序派工页面处理程序
//Copyright (c) : 通力凯顿（北京）系统集成有限公司
//Writer:wangjh
//create Date:
public class ContainerStepDispatchHandler : IHttpHandler, IRequiresSessionState
{
    private uLMSCommonIBusiness m_CommonObj = uMESBusinessFactory.instance.GetuLMSCommonObject();
    private uLMSDispatchIBusiness m_DispatchObj = uMESBusinessFactory.instance.GetuLMSDispatchObject();
    CurrentUserInfo m_UserInfo;

    public void ProcessRequest(HttpContext context)
    {
        try
        {
            var action = context.Request.Form["action"].ToString();
            m_UserInfo = (CurrentUserInfo)context.Session["CurrentUserInfo"];
            switch (action)
            {
                case "searchData":
                    SearchData(context);
                    break;
                case "getContainerDetails":
                    GetContainerDetails(context);
                    break;
                default:
                    break;
            }
        }
        catch (Exception e)
        {
            var result = new uMESProcessResult()
            {
                Result = false,
                Message = e.Message
            };
            context.Response.Write(JsonConvert.SerializeObject(result));
        }
    }
    public bool IsReusable
    {
        get
        {
            return false;
        }
    }
   
    /// <summary>
    /// 多个批次的详细
    /// </summary>
    /// <param name="context"></param>
    public void GetContainerDetails(HttpContext context)
    {
        context.Response.ContentType = "text/plain";
        uMESProcessResult result = new uMESProcessResult();
        result.Result = false;

        DataTable resultDt = new DataTable();//结果table

        //teamid和StepDispatch参数
        string teamID = "", stepDispatch = "不加";
        string strRole = m_CommonObj.GetEnterpriseAttributes("工区长角色");
        if (!m_UserInfo.Roles.Contains(strRole) && !String.IsNullOrWhiteSpace(m_UserInfo.TeamID))
        {
            teamID = m_UserInfo.TeamID;
        }

        var strSelectvalue = m_CommonObj.GetFatoryAttributes(m_UserInfo.FactoryID, "工序关联工区班组"); //获取工厂报工是否工序工区派工

        if (strSelectvalue == "是")
        {
            stepDispatch = "";
            if (string.IsNullOrWhiteSpace(m_UserInfo.TeamID))
            {
                result.Message = "该用户没有班组信息";
                context.Response.Write(JsonConvert.SerializeObject(result));
                return;
            }
        }
        //循环传参查询
        var paramDt = JsonConvert.DeserializeObject<DataTable>(context.Request.Form["params"].ToString());
        foreach (DataRow paramRow in paramDt.Rows)
        {
            Dictionary<string, string> para = new Dictionary<string, string>();
            para.Add("TogatherFlag", paramRow["togatherFlag"].ToString());
            para.Add("ProductID", paramRow["productID"].ToString());
            para.Add("WorkflowStepID", paramRow["workflowStepID"].ToString());
            para.Add("CuWorkCenterID", paramRow["cuWorkCenterID"].ToString());
            if (!string.IsNullOrWhiteSpace(paramRow["mfgOrderName"].ToString()))
            {
                para.Add("MfgOrderName", paramRow["mfgOrderName"].ToString());
            }
            para.Add("PlannedStartDate", paramRow["plannedStartDate"].ToString().Replace("T", "").Replace("0:00:00", ""));
            para.Add("PlannedCompletionDate", paramRow["plannedCompletionDate"].ToString().Replace("T", "").Replace("0:00:00", ""));
            para.Add("Status", paramRow["status"].ToString());
            para.Add("ContainerType", paramRow["containerType"].ToString());
            para.Add("FactoryID", m_UserInfo.FactoryID);
            para.Add("WorkCenterID", m_UserInfo.WorkCenterID);
            if (!string.IsNullOrWhiteSpace(teamID))
            {
                para.Add("TeamID", m_UserInfo.TeamID);
            }
            if (string.IsNullOrWhiteSpace(stepDispatch))
            {
                para.Add("StepDispatch", "");
            }
            var dt = m_DispatchObj.GetStepDispatchDetail(para);
            dt.Columns.Add("NewContaienrName");
            foreach (DataRow dr in dt.Rows)
            {
                dr["NewContaienrName"] = dr["CONTAINERNAME"].ToString().Replace(dr["productname"].ToString() + "/", "");
            }
            dt.AcceptChanges();

            resultDt.Merge(dt);
        }
        resultDt.AcceptChanges();

        result.Result = true;
        result.DBTable = resultDt;
        context.Response.Write(JsonConvert.SerializeObject(result));
    }

    /// <summary>
    /// 获取单个批次的详细
    /// </summary>
    /// <param name="context"></param>
    public void GetContainerDetail(HttpContext context)
    {
        context.Response.ContentType = "text/plain";
        uMESProcessResult result = new uMESProcessResult();
        result.Result = false;

        Dictionary<string, string> para = new Dictionary<string, string>();
        para.Add("TogatherFlag", context.Request.Form["togatherFlag"].ToString());
        para.Add("ProductID", context.Request.Form["productID"].ToString());
        para.Add("WorkflowStepID", context.Request.Form["workflowStepID"].ToString());
        para.Add("CuWorkCenterID", context.Request.Form["cuWorkCenterID"].ToString());
        para.Add("MfgOrderName", context.Request.Form["mfgOrderName"].ToString());
        para.Add("PlannedStartDate", context.Request.Form["plannedStartDate"].ToString().Replace("T00:00:00", ""));
        para.Add("PlannedCompletionDate", context.Request.Form["plannedCompletionDate"].ToString().Replace("T00:00:00", ""));
        para.Add("Status", context.Request.Form["status"].ToString());
        para.Add("ContainerType", context.Request.Form["containerType"].ToString());
        para.Add("FactoryID", m_UserInfo.FactoryID);
        para.Add("WorkCenterID", m_UserInfo.WorkCenterID);

        string strRole = m_CommonObj.GetEnterpriseAttributes("工区长角色");
        if (!m_UserInfo.Roles.Contains(strRole) && !String.IsNullOrWhiteSpace(m_UserInfo.TeamID))
        {
            para.Add("TeamID", m_UserInfo.TeamID);
        }

        var strSelectvalue = m_CommonObj.GetFatoryAttributes(m_UserInfo.FactoryID, "工序关联工区班组"); //获取工厂报工是否工序工区派工

        if (strSelectvalue == "是")
        {
            para.Add("StepDispatch", "");
            if (string.IsNullOrWhiteSpace(m_UserInfo.TeamID))
            {
                result.Message = "该用户没有班组信息";
                context.Response.Write(JsonConvert.SerializeObject(result));
                return;
            }
        }

        var dt = m_DispatchObj.GetStepDispatchDetail(para);
        dt.Columns.Add("NewContaienrName");
        foreach (DataRow dr in dt.Rows)
        {
            dr["NewContaienrName"] = dr["CONTAINERNAME"].ToString().Replace(dr["productname"].ToString() + "/", "");
        }
        dt.AcceptChanges();

        result.Result = true;
        result.DBTable = dt;
        context.Response.Write(JsonConvert.SerializeObject(result));
    }

    /// <summary>
    /// 查询获取列表
    /// </summary>
    /// <param name="context"></param>
    public void SearchData(HttpContext context)
    {
        context.Response.ContentType = "text/plain";
        uMESProcessResult result = new uMESProcessResult();
        result.Result = false;

        // m_UserInfo = (CurrentUserInfo)context.Session["CurrentUserInfo"];
        Dictionary<string, string> para = new Dictionary<string, string>();

        para.Add("CurrentPageIndex", context.Request.Form["pageNumber"].ToString());//页码
        para.Add("PageSize", context.Request.Form["pageSize"].ToString());//每页页数
        if (!string.IsNullOrWhiteSpace(context.Request.Form["containerName"].ToString()))
        {
            para.Add("ContainerName", context.Request.Form["containerName"].ToString());
        }
        if (!string.IsNullOrWhiteSpace(context.Request.Form["scanMfgorderName"].ToString()))
        {
            para.Add("mfgOrderName", context.Request.Form["scanMfgorderName"].ToString());
        }
        if (!string.IsNullOrWhiteSpace(context.Request.Form["productName"].ToString()))
        {
            para.Add("ProductName", context.Request.Form["productName"].ToString());
        }
        if (!string.IsNullOrWhiteSpace(context.Request.Form["specNo"].ToString()))
        {
            para.Add("StepNo", context.Request.Form["specNo"].ToString());
        }

        para.Add("FactoryID", m_UserInfo.FactoryID);
        para.Add("WorkCenterID", m_UserInfo.WorkCenterID);
        para["Status"] = context.Request.Form["status"].ToString();
        if (!string.IsNullOrWhiteSpace(context.Request.Form["scanContainerName"]))
        {
            var containerScan = context.Request.Form["scanContainerName"].ToString(); var tempContainerName = "";
            para.Add("ScanContainerName", context.Request.Form["scanContainerName"].ToString());
            para["ScanContainerName"] = m_CommonObj.GetScanContainerInfoReturnData(ref containerScan, ref tempContainerName);
            para["ContainerType"] = containerScan.Split(',').First();
        }

        string strRole = m_CommonObj.GetEnterpriseAttributes("工区长角色");
        if (!m_UserInfo.Roles.Contains(strRole) && !String.IsNullOrWhiteSpace(m_UserInfo.TeamID))
        {
            para.Add("TeamID", m_UserInfo.TeamID);
        }

        var strSelectvalue = m_CommonObj.GetFatoryAttributes(m_UserInfo.FactoryID, "工序关联工区班组"); //获取工厂报工是否工序工区派工

        if (strSelectvalue == "是")
        {
            para.Add("StepDispatch", "");
            if (string.IsNullOrWhiteSpace(m_UserInfo.TeamID))
            {
                result.Message = "该用户没有班组信息";
                context.Response.Write(JsonConvert.SerializeObject(result));
                return;
            }
        }
        result = m_DispatchObj.GetContainerStepDispatchDataNew(para);
        context.Response.Write(JsonConvert.SerializeObject(result));


    }
}